export const fillStyle = (style, filters)  => {
    let result = {}
    Object.keys(style).forEach((key) => {
        if (filters.includes(key)) {
          const css = stylePropToCss(key, style[key])
          result = { ...result, ...css }
        }
        
      })
      return result
}
function stylePropToCss(key, value) {
    switch(key) {
        case 'rotate':
            return  { transform: `${key}(${value}deg)` }
    }
   return { [key]: value + 'px' }
} 
/**
 * 点旋转函数
 * @param point 被旋转的点
 * @param center 旋转中心
 * @param rotate 旋转弧度
 * @returns
 */
export function rotatePoint(point, center, rotate) {
    /**
     * 旋转公式：
     *  点a(x, y)
     *  旋转中心c(x, y)
     *  旋转后点n(x, y)
     *  旋转角度θ                tan ??
     * nx = cosθ * (ax - cx) - sinθ * (ay - cy) + cx
     * ny = sinθ * (ax - cx) + cosθ * (ay - cy) + cy
     */
  
    return {
      x:
        (point.x - center.x) * Math.cos(angleToRadian(rotate)) -
        (point.y - center.y) * Math.sin(angleToRadian(rotate)) +
        center.x,
      y:
        (point.x - center.x) * Math.sin(angleToRadian(rotate)) +
        (point.y - center.y) * Math.cos(angleToRadian(rotate)) +
        center.y
    }
  }
/**
 * @description: 角度转弧度
 * Math.PI = 180 度
 */
function angleToRadian(angle) {
    return (angle * Math.PI) / 180
  }
/**
 * 请求动画帧的节流工具
 * @param fn 被执行的函数
 * @returns
 */
export function throttleFrame(fn) {
  let timer = 0
  return function (...args) {
    const self = this
    if (timer) {
      cancelAnimationFrame(timer)
      timer = 0
    }
    timer = requestAnimationFrame(() => {
      timer = 0
      fn.apply(self, args)
    })
  }
}

export function GetQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}
